# -*- coding:utf-8 -*-
'''
@File    :   basic_utils.py
@Time    :   2024/11/17 23:05:10
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''
import yaml
# import glob
import os
import sys
sys.path.append('C:\\Users\\Think\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages')
from cacheout import Cache
from pathlib import Path


#获取项目根目录绝对地址
def get_root_abpath():
    return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# 读取yaml文件
def read_yaml(filename):
    with open(filename, encoding='utf-8') as f:
        python_obj = yaml.safe_load(f)
    return python_obj


def get_yaml_data(yaml_name):
    yaml_path = get_file_path(yaml_name)  # 获取login.yaml的全路径
    result = read_yaml(yaml_path)  # 转化为python对象
    login_data = result.get('login')  # 获取字典中login的值
    for i in list(range(0, len(login_data))):  #将列表中字典元素转为元组（仅值无Key）（parameterized.expand以每个元组为每条用例）
        login_data[i] = tuple(login_data[i].values())

    return login_data  # 获取字典中login的值


#默认获取相对路径
def get_file_path(file_name, folder_name=None, Absolute=False):

    # files = glob.glob(file_name)  #以项目根目录匹配，取文件名
    #  高级匹配：匹配当前目录及其所有子目录下所有以 .txt 结尾的文件
    path = Path('.')  #项目根目录
    # files
    if (folder_name == None):
        files = list(path.glob(f'**/{file_name}'))  #按文件名匹配目录及子目录下所有的文件
    else:
        files = list(path.glob(f'**/{folder_name}/{file_name}'))  #按文件名匹配指定目录下所有的文件
    # print("高级匹配的文件：", files)
    if len(files) == 0:
        raise FileNotFoundError('No such file or directory: \'%s\'' % file_name)
    file_path = files[0]  #只返回匹配结果的第一个
    if (Absolute == True):
        return os.path.abspath(file_path)  #转为绝对路径
    return file_path


def get_path(path, file_name):
    return Path(path, file_name)


cache = Cache()


def get_cache(key):
    return cache.get(key)


def set_cache(key, value, ttl=10):
    cache.set(key, value)


if __name__ == '__main__':

    # print(read_yaml(get_file_path('login.yaml')).get('login'))
    # print(get_yaml_data('login.yaml'))
    # p = Path('.')
    # print(p.absolute())
    # path1 = Path("data")
    # path2 = "file.txt"
    # full_path = path1 / path2
    # path = Path('case', 'file.txt')
    # print(full_path == path)
    # print(full_path.__class__)
    # print(path.__class__)
    # path = Path('.')  #项目根目录
    # file_name = 'xx'
    # print(get_file_path('xx'))
    # with open("nonexistent_file.txt", "r") as file:
    #     content = file.read()
    print(1)
    pass
